import { genderStatus, eduStatus } from '@/config/defaultStatus/initStatus'
import {
  ComsTypeEnum,
  hasOptions,
  isPicTitleDescStatusArray,
  isStringArray,
  isValueStatusArray,
  type Material,
  type OptionsProps,
  type PicLink,
  type Status,
  type TextProps,
  type TypeStatus,
} from '@/types'

export function setTextStatus(textProps: TextProps, text: string) {
  textProps.status = text
}
// 添加选项
export function addOption(optionProps: OptionsProps) {
  if (isStringArray(optionProps.status)) {
    let addIndex = 1
    if (optionProps.status.length) {
      const endItem = optionProps.status[optionProps.status.length - 1]
      const endNum = Number(endItem[endItem.length - 1])
      addIndex = isNaN(endNum) ? 1 : endNum + 1
    }
    optionProps.status.push(`新选项${addIndex}`)
  } else if (isPicTitleDescStatusArray(optionProps.status)) {
    optionProps.status.push({
      value: '',
      picTitle: '',
      picDesc: '',
    })
  } else if (isValueStatusArray(optionProps.status)) {
    optionProps.status.push({
      value: '',
      status: '',
    })
  }
}

export function rmOption(optionProps: OptionsProps, index: number) {
  if (optionProps.status.length === 2) {
    return false
  }
  optionProps.status.splice(index, 1)
  return true
}

export function setCurrentStatus(optionProps: OptionsProps, index: number) {
  optionProps.currentStatus = index
}

export function setPicLinkByIndex(optionProps: OptionsProps, payload: PicLink) {
  // console.log(optionProps, index)
  if (isPicTitleDescStatusArray(optionProps.status)) {
    optionProps.status[payload.index].value = payload.link
  }
}

// 初始化预设组件store
export function updateInitStatusBeforeAdd(comStatus: Status, newMaterialName: Material) {
  if (newMaterialName === ComsTypeEnum.personInfoGender) {
    comStatus.name = ComsTypeEnum.personInfoGender
    comStatus.status.title.status = '您的性别是？'
    if (hasOptions(comStatus.status)) comStatus.status.options.status = genderStatus()
  } else if (newMaterialName === ComsTypeEnum.personInfoEdu) {
    comStatus.name = ComsTypeEnum.personInfoEdu
    comStatus.status.title.status = '您的学历是？'
    if (hasOptions(comStatus.status)) comStatus.status.options.status = eduStatus()
  }
}
